//
//  TXSwiftRefresh+Extension.swift
//  TXiOSDemoApp
//
//  Created by xicheng on 16/11/21.
//  Copyright © 2016年 xicheng. All rights reserved.
//

import UIKit

public enum TXRefreshStatus {
  
  case normal // 正常状态
  
  case refresh // 下拉刷新
  
  case loadMore // 加载更多
  
  case noMoreData // 没有更多的数据了
  
}


private var refreshStatusTag:UInt8 = 0

private var TX_headerViewClassTag:UInt8 = 0

private var TX_footerViewClassTag:UInt8 = 0


private let TXSwiftHeaderRefreshViewTag = 101

private let TXSwiftFooterViewTag = 102


extension UIScrollView {

  
  private var tx_headerViewClass:(TXSwiftAbstractHeaderView.Type)? {
    
    get {
      if let classType = objc_getAssociatedObject(self, &TX_headerViewClassTag) as? TXSwiftAbstractHeaderView.Type {
        return classType
      }
      return nil
    }
    
    set {
      objc_setAssociatedObject(self, &TX_headerViewClassTag, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
    }
    
  }
  
  private var tx_footerViewClass:(TXSwiftAbstractFooterView.Type)? {
    
    get {
      if let classType = objc_getAssociatedObject(self, &TX_footerViewClassTag) as? TXSwiftAbstractFooterView.Type {
        return classType
      }
      return nil
    }
    
    set {
      objc_setAssociatedObject(self, &TX_footerViewClassTag, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
    }
    
  }
  
  public func tx_configClass(WithHeaderClass headerClass:(TXSwiftAbstractHeaderView.Type)? , WithFooterClass footerClass:(TXSwiftAbstractFooterView.Type)?){
    self.tx_headerViewClass = headerClass
    self.tx_footerViewClass = footerClass
  }
  
  
  public var tx_refreshStatus:TXRefreshStatus {
    
    get {
      if let status = objc_getAssociatedObject(self, &refreshStatusTag) as? TXRefreshStatus {
        return status
      }
      return TXRefreshStatus.normal
    }
    
    set(newValue) {
      objc_setAssociatedObject(self, &refreshStatusTag, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
      
      
      switch newValue {
      case .normal:
        tx_headerRefreshView?.headerViewStatus = .normal
        tx_footerLoadView?.footerViewStatus = .normal
        break
      default:
        break
      }
      
    }
  
  }
  
  public var tx_headerRefreshView:TXSwiftAbstractHeaderView? {
  
    get {
      
      if let headerView = viewWithTag(TXSwiftHeaderRefreshViewTag) as? TXSwiftAbstractHeaderView {
        return headerView
      }
      return nil
    }
  }
  
  
  public func registerHeaderView(WithClass classType:TXSwiftAbstractHeaderView.Type){
    
  }
  
  
  public var tx_footerLoadView:TXSwiftFooterView? {
    
    get {
      if let footView = viewWithTag(TXSwiftFooterViewTag) as? TXSwiftFooterView {
        return footView
      }
      return nil
    }
  }
  
  public func tx_addRefreshListener(WithAction action:@escaping (()->Void)){
  
    self.alwaysBounceVertical = true
    if self.tx_headerRefreshView == nil {
      var headerView:TXSwiftAbstractHeaderView! = nil
      if self.tx_headerViewClass == nil {
        headerView = TXSwiftHeaderView.init(action:action,frame:CGRect(x:0, y: -50.0,width: self.frame.size.width,height: 50.0))
      }else {
        headerView = (self.tx_headerViewClass)!.init(action:action,frame:CGRect(x:0, y: -50.0,width: self.frame.size.width,height: 50.0))
      }      
      headerView.tag = TXSwiftHeaderRefreshViewTag
      self.addSubview(headerView)
    }
    self.tx_refreshStatus = .normal
    
  }
  public func tx_addLoadMoreListener(WithAction action:@escaping (()->Void)){
    self.alwaysBounceVertical = true
    if self.tx_footerLoadView == nil {
      var footerView:TXSwiftAbstractFooterView! = nil
      if self.tx_footerViewClass == nil {
        footerView = TXSwiftFooterView.init(action:action,frame:CGRect(x: 0, y: 0, width: self.frame.size.width, height: 44))
      }else {
        footerView = (self.tx_footerViewClass)!.init(action:action,frame:CGRect(x: 0, y: 0, width: self.frame.size.width, height: 44))
      }
      footerView.tag = TXSwiftFooterViewTag
      self.addSubview(footerView)
    }
  }
  
  /// 立刻下拉刷新
  public func tx_nowRefresh(){
    
  }
  /// 结束下拉刷新
  public func tx_endRefresh(){
    if let refreshHeaderView = self.viewWithTag(TXSwiftHeaderRefreshViewTag) as? TXSwiftAbstractHeaderView {
      refreshHeaderView.stopAnimation()
    }
    tx_refreshStatus = .normal
  }
  
  /// 结束加载更多
  public func tx_endLoadMore(){
    
  }
  
  public func tx_setNoMoreData(needHidden:Bool){
    tx_refreshStatus = .noMoreData
    guard let footerView = self.tx_footerLoadView else {
      return
    }
    footerView.footerViewStatus = .noMoreData
    let oldContentInset = footerView.originalScrollViewInsets
    if needHidden {
      footerView.isHidden = true
      self.contentInset = oldContentInset
    }else {
      footerView.isHidden = false
      self.contentInset = UIEdgeInsetsMake(oldContentInset.top, oldContentInset.left, oldContentInset.bottom + footerView.frame.height + self.frame.origin.y , oldContentInset.right)
    }
  }
  

}

